<template>
  <div>
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" title="关联源数据维护" @del="del" @newCreate="newCreate" ></v-tableTitle>
    <GTable ref="table" :hasCheck="true" :hasEdit="true" :api="apiMap.getListApi" :apiParams="apiParams" :columnSource="baseColumns" :cbFun="cbFun" @selection-change="data => tableSelectData = data"
            @setdialog="setdialog" />

    <el-dialog :title="dialogModule.title" :visible.sync="dialogModule.flag" center v-dialogDrag width="720px" @closed="closed">
      <div v-tit="'关联源数据维护'">
        <GForm ref="detail" :config="formConfig" :rules="formRules" labelWidth="120px" v-model="form"></GForm>
      </div>
      <span slot="footer">
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button @click="dialogModule.flag = false">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import tableTitle from 'commons/TableTitle';

export default {
  name: 'serialrule',
  data() {
    return {
      tableSelectData: [],
      dialogModule: {
        loading: false,
        flag: false,
        title: '',
        data: null,
        type: ''
      },
      form: {
        id: null
      },
      options:{
        datasourcekey:[],
        mainsqlField:[]
      }
    }
  },
  computed: {
    btnConfig() {
      return [
        {
          label: this.$t('common.create'),
          invalid: 'newCreate'
        },
        {
          label: this.$t('common.delete'),
          disabled: true,
          invalid: 'del'
        }
      ]
    },
    baseColumns() {
      return [
        {
          label: '源数据配置名称',
          prop: 'name',
          input: 'input'
        },
        {
          label: '源数据配置编码',
          prop: 'code',
          input: 'input'
        },
        {
          label: '数据源',
          prop: 'datasourcekey',
          input: 'select',
          options:this.options.datasourcekey
        },
        {
          label: '源数据查询sql',
          prop: 'subsql',
          input: 'input'
        }, 
        {
          label: '关联字段',
          prop: 'linkfield',
          input: 'input'
        }
      ]
    },
    formConfig() {
      return [
        {
          label: '源数据配置名称',
          prop: 'name',
          type: 'input'
        },
        {
          label: '源数据配置编码',
          prop: 'code',
          type: 'input'
        },
        {
          label: '数据源',
          prop: 'datasourcekey',
          type: 'select',
          options:this.options.datasourcekey
        },    
        {
          label: '源数据查询sql',
          prop: 'subsql',
          type: 'textarea'
        },
        {
          label: '关联源字段',
          prop: 'linkfield',
          type: 'input'
        }
      ]
    },
    formRules() {
      return {
        name: [
          {
            required: true,
            message: '源数据配置名称',
            trigger: 'blur'
          }
        ],
      }
    },
    apiMap() {
      return {
        getListApi: '/v1/PlExpressSourceDetail/selectExpressSourceDataList',
        getInfoApi:'/v1/PlExpressSourceDetail/selectExpressSourceDetailById',
        editApi:'/v1/PlExpressSourceDetail/editPlExpressSourceDetail',
        deleteApi: '/v1/PlExpressSourceDetail/deletePlExpressSourceDetailIds',
        addApi: '/v1/PlExpressSourceDetail/addPlExpressSourceDetail'
      }
    },
    apiParams() {
      return { expresssourceid: this.$route.query.id }
    }
  },
  methods: {
    handleConfirm() {
      this.$refs.detail.validate().then(valid => {
        const params = this._.cloneDeep(this.form)
        params.expresssourceid = this.$route.query.id;
        let promise
        if (this.dialogModule.type == 'edit') {
          promise = this.$post(this.apiMap.editApi, params)
        } else {
          promise = this.$post(this.apiMap.addApi, params)
        }
        promise.then(res => {
          this.$message.success(res.returnMgr)
          this.getTableData()
          this.$refs.detail?.resetFields()
          this.dialogModule.flag = false
        })
      })

    },
    getInfoById(id) {
      this.$post(this.apiMap.getInfoApi, {
        id
      }).then(res => {
        this.form = res.data.item
        this.dialogModule.flag = true
      })
    },
    setdialog(data) {
      this.dialogModule.title = `编辑[${data.name}]流水号规则`
      this.dialogModule.type = 'edit'
      this.dialogModule.data = data
      this.getInfoById(data.id)
    },
    newCreate() {
      this.dialogModule.title = '新增流水号规则'
      this.dialogModule.type = 'add'
      this.dialogModule.data = null
      this.dialogModule.flag = true
    },
    //删除
    async del(data) {
      this.$post(this.apiMap.deleteApi, data).then(res => {
        this.$message.success(res.returnMgr)
        this.getTableData()
      })
    },

    closed() {
      this.$refs.detail?.resetFields()
    },
    cbFun(res) {
      return { list: res.data.page.records, total: res.data.page.total }
    },
    async getTableData() {
      this.$refs.table.getTableData()
    }
  },
  mounted() {
       this.$post('/v1/PlExpressSourceData/selectSTDDatasourckey', { }).then(res => {
        this.options.datasourcekey = res.data.item;
      });

      this.$post('/v1/PlExpressSourceData/selectMainSqlField', { id: this.$route.query.id }).then(res => {
        this.options.mainsqlField = res.data.item;
      })
  },
  components: {
    'v-tableTitle': tableTitle
  }
}
</script>

<style scoped>
</style>
